<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class User extends MY_Controller {

    function __construct() {
        parent::__construct();
        /** 登陆验证 */
        if (!isset($_SESSION)) {
            session_start();
        }
        $user = $this->session->userdata('zjy_user');
        if (!isset($user['user_id'])) {
            $str = $_SERVER['REQUEST_URI'];
            $this->js->go('/login.html');
        } elseif (!$user['user_id']) {
            $this->js->go('/login.html');
        }

        $this->tab = 'user';
        $this->sub = 'user';
    }

    public function index() {
        self::center();
    }

    public function logout() {
        setcookie("zjy_online", "", time() - 3600 * 24 * 7, '/', $_SERVER['HTTP_HOST']);
        $this->session->set_userdata('zjy_user', '');
        $this->js->go('/login.html');
    }

    /**
     * 用户中心
     */
    public function center() {
        $s = $this->input->get('s');
        $data = array();
        $user = $this->session->userdata('zjy_user');
        $user['user_id']=intval($user['user_id']);
        $this->load->model('User_model', 'user');
        $this->load->model('User_info_model', 'user_info');
        $this->load->model('Area_model', 'area');
        $this->load->model('User_vip_model', 'user_vip');
        $this->load->model('Account_model', 'account');
        $this->load->model('Credit_model', 'credit');
        $this->load->model('Borrow_model');
        $this->load->model('User_amount_model');
        $this->load->model('Accountlog_model');

        //$this->user_vip->check_vip($user['user_id']);
        $data['user'] = $this->user->get_by_uid($user['user_id']);
        if (is_file('./static/data/avatar/' . $user['user_id'] . '_avatar_big.jpg')) {
            $data['user']['image'] = '/static/data/avatar/' . $user['user_id'] . '_avatar_big.jpg';
        } else {
            $data['user']['image'] = '/static/data/avatar/noavatar_big.jpg';
        }
        $data['user_info'] = $this->user_info->get_by_uid($user['user_id']);
//        $data['area'] = $this->area->get_list();
        $data['account'] = $this->account->get_account($user['user_id']);
        $data['credit'] = $this->credit->get_row($user['user_id']);

        if (!$data['account']) {
            $initAccount = array(
                'user_id' => $user['user_id'],
                'total' => '0',
                'use_money' => '0',
                'no_use_money' => '0',
                'collection' => '0',
            );

            $account_id = $this->account->init_account($initAccount);
            if ($account_id)
                $initAccount['id'] = $account_id;
            $data['account'] = $initAccount;
        }
        $data['user_vip'] = $this->user_vip->get_by_uid($user['user_id']);
        if (isset($data['user_vip']['user_id']) && $data['user_vip']['vip_status'] == 1 && $data['user_vip']['kefu_userid'] == 0) {
            //$this->js->go('/user/vip.html');
        }
        //信用等级 user_credit
        $this->load->model('Credit_model');
        $user_credit_obj = $this->Credit_model->get_row($user['user_id']);
        $user_credit_obj['credit_value'] = isset($user_credit_obj['credit_value']) ? $user_credit_obj['credit_value'] : 0;
        $data['credit_value'] = $user_credit_obj['credit_value'];
        $user_credit_img = $this->Credit_model->get_pic($user_credit_obj['credit_value']);
        $data['user_credit_img'] = $user_credit_img;
        //代收
        $collection_list = $this->Borrow_model->collection_total($user['user_id'], 0);
        //代收总额
        $data['collection_total'] = $collection_list['collection_total'];
        //代收本金
        $data['collection_capital'] = $collection_list['collection_capital'];
        //代收利息
        $data['collection_interest'] = $collection_list['collection_interest'];
        //最近回款时间
        $data['first_collection_time'] = $this->Borrow_model->first_collection_time($user['user_id']);

        $collection_list1 = $this->Borrow_model->collection_total($user['user_id'], 1);
        //净赚利息
        $data['ycollection_interest'] = $collection_list1['collection_interest'];

        //当月收益
        $collection_month_list = $this->Borrow_model->collection_month_total($user['user_id'], 1);
        $data['tcollection_interest'] = $collection_month_list['collection_interest'];

        //借款项目
        $repayment_list = $this->Borrow_model->get_repayment_list(
            array(
                'where' => array('user_id'=>$user['user_id']),
                'page_size' => 3,
                'page_start' => 0
            )
        );
        $data['repayment_list'] = $repayment_list['repayment_list'];
        //借款本金
        $repayment_list_total = $this->Borrow_model->repayment_total($user['user_id']);
        //借款总额
        $data['repayment_total'] = $repayment_list_total['repayment_account'];
        $yrepayment_list_total = $this->Borrow_model->repayment_total($user['user_id'], 1);
        //已还总额
        $data['yrepayment_total'] = $yrepayment_list_total['repayment_yesaccount'];

        //收益总计
        $data['income_total'] = $this->account->income_total($user['user_id']);

        //未还总额
        $loan_repayment_total = $this->Borrow_model->loan_repayment_total($user['user_id'], 0);
        $data['wrepayment_total'] = $loan_repayment_total['repay_account'];

        //净值额度
        $no_repayment_total = $this->Borrow_model->no_repayment_borrow_3($user['user_id']); //未还的净值标总额
        $data['jing_amount'] = ($data['account']['collection'] + $data['account']['use_money'] - $no_repayment_total) * CON_BORROW_JIN_CREDIT;

        //用户投的标
        $param = array(
            'where'=>array(
                'user_id'=>$user['user_id'],
                'fstatus'=>1,
            ),
            'page_size'=>3,
            'page_start'=>0,
        );
        $data['user_tender'] = $this->Borrow_model->borrow_tender_user_list($param);

        //用户发的标
        $param = array(
            'where'=>array(
                'user_id'=>$user['user_id'],
                'fstatus'=>3,
            ),
            'page_size'=>3,
            'page_start'=>0,
        );
        $data['user_borrow'] = $this->Borrow_model->get_borrow_user_list($param);


        $first_repayment_obj = $this->Borrow_model->first_repayment_time($user['user_id']);
        $data['first_repayment_time'] = 0;
        $data['first_repayment_account'] = 0;
        if (count($first_repayment_obj) > 0) {
            $data['first_repayment_time'] = $first_repayment_obj['repayment_time'];
            $data['first_repayment_account'] = $first_repayment_obj['repayment_account'];
        }
        if (isset($data['user_vip']['vip_status']) && $data['user_vip']['vip_status'] == 1) {
            $kefu_list = $this->user->get_kefu_list();
            $data['kefu'] = isset($kefu_list[$data['user_vip']['kefu_userid']]) ? $kefu_list[$data['user_vip']['kefu_userid']] : array();
        }

        $data['award_total'] = $this->Accountlog_model->get_sum_type($user['user_id'], AWARD_ADD);
        $data['repayment_tender_reward'] = $this->Accountlog_model->get_sum_type($user['user_id'], REPAYMENT_TENDER_REWARD);
        //用户额度
        $data['user_amount'] = $this->User_amount_model->get_one($user['user_id']);

        $borrow_list_tmp = $this->Borrow_model->get_borrow_list(array('select' => 1, 'list' => 1, 'page_size' => 3, 'page_start' => 0));
        $borrow_list = array();
        foreach ($borrow_list_tmp as $borrow) {
            $user_obj = $this->user->get_by_uid($borrow['user_id']);
            $borrow['username'] = $user_obj['username'];
            $borrow_list[] = $borrow;
        }
        $data['borrow_list'] = $borrow_list;
        $data['title'] = "用户中心" . ' - ' . CON_KEYWORDS;
        $data['s'] = $s;

        $data['tab'] = $this->tab;
        $data['sub'] = 'usercenter';
        $data['user_tab'] = 'center';

        $userinfo = $this->user->get_by_uid($user['user_id']);

        $safeinfo = $this->safe_leve($userinfo);

        $data['addup'] = $safeinfo['addup'];
        $this->load->view('user/user_center', $data);
    }

    /**
     * 实名认证
     */
    public function realname() {
        $data = array();
        $act = $this->input->post('act');
        $user = $this->session->userdata('zjy_user');

        $this->load->model('User_model', 'user');
        $this->load->model('Area_model', 'area');
        $this->load->model('Linkage_model', 'linkage');
        $this->load->model('User_info_model', 'user_info');

        if ($act == 'submit') {
            $ret = array(
                'msg' => '',
                'url' => '',
            );

            $setArr = array(
                'realname' => $this->input->post('realname'),
                'sex' => $this->input->post('sex'),
                'nation' => $this->input->post('nation'),
                'birthday' => $this->input->post('birthday'),
                'card_type' => $this->input->post('card_type'),
                'card_id' => $this->input->post('card_id'),
                'province' => $this->input->post('province'),
                'city' => $this->input->post('city'),
                'area' => $this->input->post('area'),
            );
            $setArr2 = array(
                'province' => $this->input->post('province2'),
                'city' => $this->input->post('city2'),
                'area' => $this->input->post('area2'),
            );
            //get_by_uid
            if (!$setArr['realname']) {
                $ret['msg'] = '请填写真实姓名';
            } elseif (!$setArr['sex']) {
                $ret['msg'] = '请选择性别';
            } elseif (!$setArr['nation']) {
                $ret['msg'] = '请选择名族';
            } elseif (!$setArr['birthday']) {
                $ret['msg'] = '请填写生日';
            } elseif (!$setArr['card_type']) {
                $ret['msg'] = '请选择证件类型';
            } elseif (!$setArr['card_id']) {
                $ret['msg'] = '请填写证件号码';
            } elseif (!$setArr['province']) {
                $ret['msg'] = '请选择省份';
            } elseif (!$setArr['city']) {
                $ret['msg'] = '请选择市区';
            }/* elseif(!$this->myformcheck->isChinese($setArr['realname'])){
              $ret['msg'] = '请填写有效的真实姓名';
              } */ elseif (!$this->myformcheck->isDate($setArr['birthday'])) {
                $ret['msg'] = '请填写有效的生日日期';
            } else {
                $isE = $this->user->get_one(array('card_id' => $setArr['card_id'], 'user_id !=' => $user['user_id']), 'user_id');
                if ($isE) {
                    $ret['msg'] = '证件号码已存在';
                } else {
                    if (!$_FILES['card_pic1']['name'] || !$_FILES['card_pic2']['name']) {
                        $ret['msg'] = '请上传证件图片';
                    }
                }
            }

            if ($ret['msg']) {
                //echo json_encode($ret);exit;
                $this->js->alert($ret['msg']);
                exit;
                //$this->js->go('/user/realname');
            }

            $setArr['birthday'] = strtotime($setArr['birthday']);

            $config['upload_path'] = 'static/uploads/';
            $config['allowed_types'] = 'jpg|png';
            $config['file_name'] = date('Y-m-d') . '_' . $user['user_id'] . '_1';
            $config['overwrite'] = true;
            $config['max_size'] = '2048';
            $config['max_width'] = '2048';
            $config['max_height'] = '3000';
            $this->load->library('upload', $config);
            $this->load->library('image_lib');
            if (isset($_FILES['card_pic1']) && $_FILES['card_pic1']['name'] != '') {
                if ($this->upload->do_upload('card_pic1')) {
                    $tmp = $this->upload->data();
                    $setArr['card_pic1'] = 'static/uploads/' . $config['file_name'] . $tmp['file_ext'];
                    //添加水印
                    $warter['source_image'] = $setArr['card_pic1'];
                    $warter['wm_type'] = 'overlay';
                    $warter['wm_overlay_path'] = WATERMARK_IMG;
                    $warter['wm_opacity'] = '60';
                    $warter['wm_vrt_alignment'] = 'middle';
                    $warter['wm_hor_alignment'] = 'center';
                    $this->image_lib->initialize($warter);
                    $this->image_lib->watermark();
                } else {
//                    var_dump($config);exit;
                    $ret['msg'] = '上传图片失败';
                    $this->js->alert($ret['msg']);
                    exit;
                    //echo json_encode($ret);exit;
                }
            }
            if (isset($_FILES['card_pic2']) && $_FILES['card_pic2']['name'] != '') {
                $config['file_name'] = date('Y-m-d') . '_' . $user['user_id'] . '_2';
                $this->upload->initialize($config);
                if ($this->upload->do_upload('card_pic2')) {
                    $tmp = $this->upload->data();
                    $setArr['card_pic2'] = 'static/uploads/' . $config['file_name'] . $tmp['file_ext'];
                    //添加水印
                    $warter['source_image'] = $setArr['card_pic2'];
                    $warter['wm_type'] = 'overlay';
                    $warter['wm_overlay_path'] = WATERMARK_IMG;
                    $warter['wm_opacity'] = '60';
                    $warter['wm_vrt_alignment'] = 'middle';
                    $warter['wm_hor_alignment'] = 'center';
                    $this->image_lib->initialize($warter);
                    $this->image_lib->watermark();
                } else {
                    $ret['msg'] = '上传图片失败';
                    $this->js->alert($ret['msg']);
                    exit;
                    //echo json_encode($ret);exit;
                }
            }
            $setArr['real_status'] = 2;
            $this->user->update($setArr, array('user_id' => $user['user_id']));

            $tmpUserInfo = $this->user_info->get_by_uid($user['user_id']);
            if ($tmpUserInfo) {
                $this->user_info->update($setArr2, array('user_id' => $user['user_id']));
            } else {
                $setArr2['user_id'] = $user['user_id'];
                $this->user_info->add($setArr2);
            }

            $ret['msg'] = '操作成功，等待管理员审核';
            $this->js->alert($ret['msg']);
            exit;
            //echo json_encode($ret);exit;
        } else {
            $data = $this->get_user_info('user_realname', 'safe');
//            var_dump($data);exit;
            //$data['user'] = $this->user->get_by_uid($user['user_id']);
            $data['user_info'] = $this->user_info->get_by_uid($user['user_id']);
            $data['area'] = $this->area->get_list();
            $data['minzu'] = $this->linkage->get_linkage_list(31);
            $data['title'] = "实名认证" . ' - ' . CON_KEYWORDS;
            $data['v'] = $this->user->arrays();
            $data['tab_first'] = 'account';
            $data['tab_second'] = 'account_auth';
            $this->load->view('user/user_realname', $data);
        }
    }

    /**
     * 邮箱认证
     */
    public function email() {
        $data = array();
        $act = $this->input->post('act');
        $user = $this->session->userdata('zjy_user');
        $user_id = $user['user_id'];
        $this->load->model('User_model', 'user');
        $data = $this->get_user_info('user_email', 'safe');

        //$data['user'] = $this->user->get_by_uid($user_id);
        if ($act == 'submit') {
            $ret = array(
                'msg' => '',
                'url' => '',
            );
            $email = $this->input->post('email');
            if ($data['user']['email_status'] == 1) {
                $ret['msg'] = '邮箱验证已通过';
                echo json_encode($ret);
                exit;
            }

            if (!$email) {
                $ret['msg'] = '请输入有效邮箱';
            } elseif (!$this->myformcheck->isEmail($email)) {
                $ret['msg'] = '请输入有效邮箱';
            } else {
                $where = array(
                    'user_id !=' => $user_id,
                    'email' => $email
                );
                if ($this->user->get_one($where, 'user_id')) {
                    $ret['msg'] = '该邮箱已存在';
                }
            }

            if ($ret['msg']) {
                echo json_encode($ret);
                exit;
            }

            $setArr = array(
                'email' => $email,
                'email_status' => 2,
            );
            $this->user->update($setArr, array('user_id' => $user_id));

            $res = $this->Notice(1, $user_id);

            if ($res) {
                echo json_encode($ret);
                //$this->js->alert('邮件已发送，请根据邮件提示完成邮箱认证');
                exit;
            } else {
                $ret['msg'] = '邮件发送失败，请联系客服';
                echo json_encode($ret);
                exit;
            }
        } else {
            $data['title'] = "邮箱认证" . ' - ' . CON_KEYWORDS;
            $data['tab_first'] = 'account';
            $data['tab_second'] = 'account_auth';
            $this->load->view('user/user_email', $data);
        }
    }

    /**
     * 手机认证
     */
    public function phone() {
        $user = $this->session->userdata('zjy_user');
        $phone = $this->input->post('phone');
        $this->load->model('User_model', 'user');
        $user_id = $user['user_id'];
        $this->mycache->delete('zjy_user_' . $user_id);
        $data['user'] = $this->user->get_by_uid($user_id);
        $ret = array(
            'msg' => '',
            'url' => '',
        );

        if ($this->input->post('act') == 'apply') {//提交
            $k = md5('gsddx');
            if (isset($_COOKIE[$k]) && $_COOKIE[$k] == 1) {
                $ret['msg'] = '5分钟内只能获取一次短信验证码，请稍后再试';
                echo json_encode($ret);
                exit;
            } else {
                if (!$phone) {
                    $ret['msg'] = '请填写手机号码';
                } elseif (!$this->myformcheck->isPhone($phone)) {
                    $ret['msg'] = '请填写有效的手机号码';
                } else {
                    // 验证手机号码是否存在
                    $tmpwhere = array(
                        'phone' => $phone,
                        'user_id !=' => $user_id
                    );
                    $row = $this->user->get_one($tmpwhere, 'user_id');
                    if (isset($row['user_id'])) {
                        $ret['msg'] = '该手机号码已存在';
                    }
                }
                if ($ret['msg']) {
                    echo json_encode($ret);
                    exit;
                }


                $setArr = array(
                    'phone' => $phone,
                    'phone_status' => 2,
                );

                $this->user->update($setArr, array('user_id' => $user_id));
                $md5 = md5($user_id . $setArr['phone']);
                $md5 = substr($md5, 3, 6);

                $res = $this->Notice(11, $user_id, array('vcode' => $md5));
                if ($res) {
                    //$this->input->set_cookie('gsddx',1);
                    $ctime = time() + 5 * 60; //300秒内只能发送一次短信息
                    setcookie($k, 1, $ctime);
                    $ret['msg'] = '手机验证码已发送，请注意查收';
                    echo json_encode($ret);
                    exit;
                } else {
                    $ret['msg'] = '短信发送失败，请稍后再试或联系客服';
                    echo json_encode($ret);
                    exit;
                }
            }
        } elseif ($this->input->post('act') == 'verify') {//验证
            $verify_code = $this->input->post('verify_code');
            $md5 = substr(md5($user_id . $data['user']['phone']), 3, 6);
            if ($verify_code == $md5) {
                $setArr = array(
                    'phone_status' => 1,
                );
                //修改手机号码
                $setArr['phone'] = $phone;
                $res = $this->user->update($setArr, array('user_id' => $user_id));
                if ($res) {
                    $this->Credit($user_id, 'phone');

                    $ret['msg'] = '验证通过';
                    echo json_encode($ret);
                    exit;
                } else {
                    $ret['msg'] = '网络忙，请稍后再试！';
                    echo json_encode($ret);
                    exit;
                }
            } else {
                $ret['msg'] = '验证码不正确';
                echo json_encode($ret);
                exit;
            }
        } else {//展示页面
            $data = $this->get_user_info('user_phone', 'safe');

            $data['title'] = "手机认证" . ' - ' . CON_KEYWORDS;
            $data['tab_first'] = 'account';
            $data['tab_second'] = 'account_auth';
            $this->load->view('user/user_phone', $data);
        }
    }

    /**
     * 上传头像
     */
    public function photo($act = '') {
        $user = $this->session->userdata('zjy_user');
        $this->load->model('User_model', 'user');
        $user_id = $user['user_id'];
        $data['user'] = $this->user->get_by_uid($user_id);

        if ($act == 'upfile') {
            $file_src = "src.png";
            $filename162 = "./static/data/avatar/" . $user_id . "_avatar_big.jpg";
            $filename48 = "./static/data/avatar/" . $user_id . "_avatar_middle.jpg";
            $filename20 = "./static/data/avatar/" . $user_id . "_avatar_small.jpg";

            $src = base64_decode($_POST['pic']);
            $pic1 = base64_decode($_POST['pic1']);
            $pic2 = base64_decode($_POST['pic2']);
            $pic3 = base64_decode($_POST['pic3']);

            if ($src) {
                file_put_contents($file_src, $src);
            }

            file_put_contents($filename162, $pic1);
            file_put_contents($filename48, $pic2);
            file_put_contents($filename20, $pic3);

            $rs['status'] = 1;

            print json_encode($rs);
        } else {
            $data = $this->get_user_info('user_photo', 'safe');

            if (is_file('./static/data/avatar/' . $user['user_id'] . '_avatar_big.jpg')) {
                $data['photo'] = '/static/data/avatar/' . $user['user_id'] . '_avatar_big.jpg';
            } else {
                $data['photo'] = '/static/data/avatar/noavatar_big.jpg';
            }
            $data['title'] = "上传头像" . ' - ' . CON_KEYWORDS;
            $data['tab_first'] = 'account';
            $data['tab_second'] = 'account_auth';
            $this->load->view('user/user_photo', $data);
        }
    }
    function photo_upload(){
//        var_dump($_FILES);exit;
//        var_dump($_SESSION);exit;
        $user=$this->session->userdata('zjy_user');
        $config['upload_path'] = 'static/data/avatar/';
        $config['allowed_types'] = 'jpg|png';
        $config['file_name'] = $user['user_id'] . '_avatar_big.jpg';
        $config['overwrite'] = true;
        $config['max_size'] = '2048';
        $config['max_width'] = '2048';
        $config['max_height'] = '3000';
        $this->load->library('upload', $config);
        $this->load->library('image_lib');
        if (isset($_FILES['photo']) && $_FILES['photo']['name'] != '') {
            if ($this->upload->do_upload('photo')) {
                $tmp = $this->upload->data();
//                var_dump($tmp);exit;
                $setArr['photo'] = 'static/data/avatar/' . $config['file_name'];
                //添加水印
//                $warter['source_image'] = $setArr['photo'];
//                $warter['wm_type'] = 'overlay';
//                $warter['wm_overlay_path'] = WATERMARK_IMG;
//                $warter['wm_opacity'] = '60';
//                $warter['wm_vrt_alignment'] = 'middle';
//                $warter['wm_hor_alignment'] = 'center';
//                $this->image_lib->initialize($warter);
                $this->image_lib->watermark();
                echo "<script>alert('提交成功！');window.location.href='/user/center.html';</script>";
            } else {
//                    var_dump($config);exit;
                $ret['msg'] = '上传图片失败';
                $this->js->alert($ret['msg']);
                exit;
                //echo json_encode($ret);exit;
            }
        }
    }
    /**
     * VIP认证
     */
    public function vip() {
        $user = $this->session->userdata('zjy_user');
        $user_id = $user['user_id'];
        $user = $this->user_model->get_by_uid($user_id);
        $username = $user['username'];
        $act = $this->input->post('act');
        $this->mycache->delete('zjy_user_vip_' . $user_id);
        $this->load->model('User_vip_model', 'vip');


        if ($act == 'submit') {
            $ret = array(
                'msg' => '',
                'url' => '',
            );
            $kefu_userid = $this->input->post('kefu_userid');
            if ($kefu_userid < 1) {
//                $this->js->alert('请选择客服');
//                $this->js->go('/user/vip');
                $ret['msg'] = '请选择客服.';
                echo json_encode($ret);
                exit;
            }

            $time = time();
            $ip = $this->input->ip_address();
            $setArr = array(
                'user_id' => $user_id,
                'kefu_userid' => $kefu_userid,
                'kefu_addtime' => $time,
                'vip_status' => 0,
                'vip_remark' => '',
                'vip_money' => VIP_MONEY,
                'vip_verify_remark' => '',
                'vip_verify_time' => $time,
            );

            $row = $this->vip->get_by_uid($user_id);
            if ($row && $row['vip_status'] == 1 && $row['kefu_userid'] > 0 && $row['vip_verify_time'] > $time - 3600 * 24 * 365) {
                $this->js->alert('您已经是VIP');
                $this->js->go('/user/vip');
                exit;
            } elseif ($row && $row['vip_status'] == 1 && $row['kefu_userid'] == 0) {
                $setArr = array(
                    'kefu_userid' => $kefu_userid
                );
                $this->vip->update($setArr, array('user_id' => $user_id));
                $this->js->alert('选择专属客服成功');
                $this->js->go('/user/vip');
                exit;
            } else {
                if (VIP_MONEY > 0) {
                    $setArr['vip_verify_remark'] = 'VIP审核通过';
                    $setArr['vip_status'] = 1; //自动通过无需人工审核
                    //判断钱够不够
                    $this->load->model('Account_model');
                    $user_account = $this->Account_model->get_account($user_id);
                    if ($user_account['use_money'] < VIP_MONEY) {
//                        $this->js->alert('您的余额不足,请先充值');
//                        $this->js->go('/account/recharge');
                        $ret['msg'] = '您的余额不足,请先充值.';
                        echo json_encode($ret);
                        exit;
                    } else {
//                        $mmmconfig = array();
//                        $mmmconfig[] = array(
//                            'fromUserId' => $user_id, //付款人userId
//                            'fromMmm' => $user['mmm_id'], //付款人乾多多标识
//                            'toUserId' => '', //收款人userId
//                            'toMmm' => MONEYMOREMOREPID, //收款人乾多多标识
//                            'batchNo' => 'REVIP', //网贷平台标号
//                            'amount' => VIP_MONEY, //转账金额
//                            'fullAmount' => VIP_MONEY, //总转账金额
//                            'transferName' => 'VIP费用扣除', //用途
//                            'remark' => 'VIP费用扣除', //备注
//                        );
//                        $this->noticeMmm($mmmconfig, 'REVIP', $user_id . '-' . $kefu_userid . '-' . $user['username'] . '-' . $ip);
                        //事务开始
                        $this->db->trans_begin();
                        //扣除会员费188  VIP
                        $log['user_id'] 		= $user_id;
                        $log['ftype'] 			= VIP;
                        $log['money'] 			= 188;
                        $log['total'] 			= $user_account['total']-$log['money'];
                        $log['use_money'] 		= $user_account['use_money']-$log['money'];
                        $log['no_use_money'] 	= $user_account['no_use_money'];
                        $log['collection'] 		= $user_account['collection'];
                        $log['to_user'] 		= 1;
                        $log['remark'] 			= "VIP费用扣除";
                        $log['add_time'] 		= $time;
                        $log['addip'] 			= $ip;
                        $mode = $user_id%100;
                        $res = $this->db->insert('zjy_account_log_'.$mode, $log);
                        if ($res){
                            //更新用户资金表
                            $res = $this->db->query("update zjy_account set total=total-{$log['money']},use_money=use_money-{$log['money']} where user_id={$user_id}");
                        }
                        if ($res){
                            if ($row){
                                //修改
                                $res = $this->vip->update($setArr,array('user_id'=>$user_id));
                            }else{
                                //添加
                                $res = $this->vip->apply($setArr);
                            }
                        }

                        //提交事务
                        if($res){
                            $this->db->trans_commit();
                        }else{
                            $this->db->trans_rollback();
                        }

                        $ret['msg'] = $this->db->trans_status() ? 'VIP审核通过！' : '操作失败，请联系客服';
                        echo json_encode($ret);
                        exit;
                    }
                } else {
                    $setArr['vip_status'] = 2;

                    $res = $this->vip->apply($setArr);
                    if ($res) {
                        $this->letterStation($kefu_userid, 'VIP申请', '用户 ' . $username . ' 申请vip');
                        $this->js->alert('VIP申请已提交，请等待审核。');
                        $this->js->go('/user/vip');
                        exit;
                    } else {
                        $this->js->alert('操作失败，请联系客服');
                        $this->js->go('/user/vip');
                        exit;
                    }
                }
            }
        } else {
            $data = array();
            $data = $this->get_user_info('user_vip', 'safe');

            $data['vipinfo'] = $this->vip->get_by_uid($user_id);
            //if(!isset($data['vipinfo']['vip_status'])||$data['vipinfo']['kefu_userid']==0){
            $this->load->model('User_model', 'user');
            $data['kefu_list'] = $this->user->get_kefu_list();
            //}
            $data['title'] = "VIP认证" . ' - ' . CON_KEYWORDS;
            $data['tab_first'] = 'account';
            $data['tab_second'] = 'account_vip';
            $this->load->view('user/user_vip', $data);
        }
    }

    /**
     * 隐私设置
     */
    function privacy() {
        $data = array();
        $act = $this->input->post('act');
        $user = $this->session->userdata('zjy_user');
        $this->load->model('User_model');
        $user_id = $user['user_id'];
        if ($act == 'submit') {
            $setArr = array(
                'friends' => $this->input->post('friends'),
                'message' => $this->input->post('message'),
                'tender' => $this->input->post('tender'),
                'borrow' => $this->input->post('borrow'),
                'money' => $this->input->post('money'),
                'reply' => $this->input->post('reply'),
                'ad' => $this->input->post('ad'),
                'vip_repay' => $this->input->post('vip_repay'),
            );

            $setArr = json_encode($setArr); //var_dump($setArr);exit;
            $this->User_model->update(array('privacy' => $setArr), array('user_id' => $user_id));
            echo "保存成功";
            exit;
        } else {
            $data = $this->get_user_info('user_privacy', 'privacy');
            $user = $this->User_model->get_by_uid($user_id);
            $data['privacy'] = $user['privacy'] ? json_decode($user['privacy'], true) : array();
            $data['title'] = "隐私设置" . ' - ' . CON_KEYWORDS;
            $data['tab_first'] = 'account';
            $data['tab_second'] = 'account_auth';
            $this->load->view('user/user_privacy', $data);
        }
    }

    /**
     * 用户详细信息
     * @param string $act
     */
    public function info($act = '') {
        $data = array();

        $submit = $this->input->post('submit');
        $user = $this->session->userdata('zjy_user');
//        var_dump($user);exit;
        $user_id = $user['user_id'];

        $this->load->model('User_model', 'user');
        $this->load->model('User_info_model', 'userinfo');
        $data = $this->get_user_info('', 'user_base');

        //$data['user'] = $this->user->get_by_uid($user_id);
        $data['userinfo'] = $this->userinfo->get_by_uid($user_id);
        $data['act'] = $act;

        if ($act == '' || $act == 'index') {
            if ($submit == 1) {
                $setArr = array(
                    'user_desc' => $this->input->post('user_desc'),
                );
                $setUserArr = array(
//                    'username' => $this->input->post('username'),
                    'sex' => $this->input->post('sex'),
                    'province' => $this->input->post('province'),
                    'city' => $this->input->post('city'),
                    'area' => $this->input->post('area')
                );
                //如果名字在数据库中已存在且名字不是当前用户的名字
//                if($this->user->get_one(array('username' => $setUserArr['username']),"*") && $setUserArr['username'] != $user['username']) {
//                    $ret['msg'] = '用户名已存在';
//                    $ret['url'] = '';
//                    echo json_encode($ret);
//                    exit;
//                }
                if ($data['userinfo']) {
                    $this->db->trans_start();
                    $this->userinfo->update($setArr, array('user_id' => $user_id));
                    $this->user->update($setUserArr,array('user_id' => $user_id));
                    $this->db->trans_complete();
                    $res = $this->db->trans_status();
                } else {
                    $setArr['user_id'] = $user_id;
                    $res = $this->userinfo->add($setArr);
                }
                $ret['msg'] = $res ? '个人基本信息提交成功' : '修改失败';
                $ret['url'] = '';
                echo json_encode($ret);
                exit;
            }
            $this->load->model('Area_model', 'area');
            $data['area'] = $this->area->get_list();
            $data['user_tab'] = 'userinfo_peason';
            $data['title'] = "个人基本信息" . ' - ' . CON_KEYWORDS;
        } elseif ($act == 'contact') {
            if ($submit == 1) {
                $setArr = array(
                    'education_record' => intval($this->input->post('education_record')),
                    'education_school' => $this->input->post('education_school'),
                    'education_time1' => strtotime($this->input->post('education_time1')),
                    'education_time2' => strtotime($this->input->post('education_time2')),
                    'marry' => intval($this->input->post('marry')),
                    'child' => intval($this->input->post('child')),
                    'safe' => intval($this->input->post('safe')),
                    'house' => intval($this->input->post('house')),
                    'car' => intval($this->input->post('car')),
                    'province' => intval($this->input->post('province')),
                    'city' => intval($this->input->post('city')),
                    'area' => intval($this->input->post('area')),
                    'address' => $this->input->post('address'),
                    'tel' => $this->input->post('tel'),
                    'qq' => $this->input->post('qq'),
                    'wangwang' => $this->input->post('wangwang'),
                );


                if ($data['userinfo']) {
                    $res = $this->userinfo->update($setArr, array('user_id' => $user_id));
                } else {
                    $setArr['user_id'] = $user_id;
                    $res = $this->userinfo->add($setArr);
                }


                $this->user->update(array('qq' => $setArr['qq']), array('user_id' => $user_id));

                $ret['msg'] = $res ? '个人信息提交成功' : '无修改或者修改失败';
                //$ret['msg'] = $setArr;
                $ret['url'] = '';
                echo json_encode($ret);
                exit;
            }
            $data['user_tab'] = 'userinfo_contact';
            $data['title'] = "个人详细信息" . ' - ' . CON_KEYWORDS;
        } elseif ($act == 'company') {
            if ($submit == 1) {
                $setArr = array(
                    'company_name' => $this->input->post('company_name'),
                    'company_type' => intval($this->input->post('company_type')),
                    'company_office' => intval($this->input->post('company_office')),
                    'income' => intval($this->input->post('income')),
                    'company_employee' => $this->input->post('company_employee'),
                    'company_tel' => $this->input->post('company_tel'),
                    'company_address' => $this->input->post('company_address'),
                    'company_weburl' => $this->input->post('company_weburl'),
                    'linkman1' => $this->input->post('linkman1'),
                    'relation1' => intval($this->input->post('relation1')),
                    'phone1' => $this->input->post('phone1'),
                    'linkman2' => $this->input->post('linkman2'),
                    'relation2' => intval($this->input->post('relation2')),
                    'phone2' => $this->input->post('phone2'),
                );
                if ($data['userinfo']) {
                    $res = $this->userinfo->update($setArr, array('user_id' => $user_id));
                } else {
                    $setArr['user_id'] = $user_id;
                    $res = $this->userinfo->add($setArr);
                }
                $ret['msg'] = $res ? '工作信息提交成功' : '无修改或者修改失败';
                $ret['url'] = '';
                echo json_encode($ret);
                exit;
            }
            $data['user_tab'] = 'userinfo_company';
            $data['title'] = "工作认证信息" . ' - ' . CON_KEYWORDS;
        } elseif ($act == 'qiye') {
            if ($submit == 1) {
                $setArr = array(
                    'qiye_type' => $this->input->post('qiye_type'),
                    'qiye_name' => $this->input->post('qiye_name'),
                    'qiye_employee' => $this->input->post('qiye_employee'),
                    'zhuce' => $this->input->post('zhuce'),
                    'fanwei' => $this->input->post('fanwei'),
                    'nianjian' => $this->input->post('nianjian'),
                    'qiye_background' => $this->input->post('qiye_background'),
                );
                if ($data['userinfo']) {
                    $res = $this->userinfo->update($setArr, array('user_id' => $user_id));
                } else {
                    $setArr['user_id'] = $user_id;
                    $res = $this->userinfo->add($setArr);
                }
                $ret['msg'] = $res ? '企业认证信息提交成功' : '无修改或者修改失败';
                $ret['url'] = '';
                echo json_encode($ret);
                exit;
            }
            $data['user_tab'] = 'userinfo_qiye';
            $data['title'] = "企业认证信息" . ' - ' . CON_KEYWORDS;
        } else {
            $this->js->alert('页面不存在');
            $this->js->go('/user/info/index.html');
        }
        //二级tab点亮
        $data['tab_first'] = 'account';
        $data['tab_second'] = 'account_base';
        $this->load->view('user/user_info', $data);
    }

    public function pwd() {
        $act = $this->input->post('act');
        if ($act == 'submit') {
            $ret = array('msg' => '', 'url' => '');
            $oldpwd = $this->input->post('oldpwd');
            $newpwd = $this->input->post('newpwd');
            $newpwd2 = $this->input->post('newpwd2');

            if (!isset($newpwd[5])) {
                $ret['msg'] = '新密码太短';
                echo json_encode($ret);
                exit;
            }

            if ($newpwd == $newpwd2) {
                $user = $this->session->userdata('zjy_user');
                $user_id = $user['user_id'];
                $this->load->model('User_model', 'user');
                $user = $this->user->get_by_uid($user_id);

                if (md5(md5($oldpwd)) == $user['password']) {
                    $res = $this->user->update(array('password' => md5(md5($newpwd))), array('user_id' => $user_id));
                    if ($res) {

                        $ret['url'] = '/user/logout/index.html';
                        echo json_encode($ret);
                        exit;
                    } else {
                        $ret['msg'] = '修改失败';
                        echo json_encode($ret);
                        exit;
                    }
                } else {
                    $ret['msg'] = '原始密码错误';
                    echo json_encode($ret);
                    exit;
                }
            } else {
                $ret['msg'] = '两次密码输入不一致';
                echo json_encode($ret);
                exit;
            }
        }

        $data = $this->get_user_info('user_pwd', 'pwd');

        $data['title'] = "密码设置" . ' - ' . CON_KEYWORDS;
        $this->load->view('user/user_pwd', $data);
    }

    public function paypwd() {
        if (!isset($_SESSION)) {
            session_start();
        }
        $this->load->helper('captcha');
        $act = $this->input->post('act');

        $user = $this->session->userdata('zjy_user');
        $user_id = $user['user_id'];
        $this->load->model('User_model', 'user');
        $user = $this->user->get_by_uid($user_id);

        $data['user'] = $user;

        if ($act == 'submit') {
            $ret = array('msg' => '', 'url' => '');
            $captcha = $this->input->post('captcha');

            if ($captcha == "") {
                $ret['msg'] = '验证码不能为空';
                echo json_encode($ret);
                exit;
            } elseif ($captcha != $_SESSION['captcha']) {
                $ret['msg'] = '验证码不正确';
                echo json_encode($ret);
                exit;
            } else {
                $oldpwd = $this->input->post('oldpwd');
                $newpwd = $this->input->post('newpwd');
                $newpwd2 = $this->input->post('newpwd2');

                if (!isset($newpwd[5])) {
                    $ret['msg'] = '新密码太短';
                    echo json_encode($ret);
                    exit;
                }
                if ($newpwd == $newpwd2) {
                    $tmpPwd = $user['paypassword'] ? $user['paypassword'] : $user['password'];
                    //获取第三方数据信息，检查是否来自第三方
                    $this->load->model('Oauth_model', 'oauth');
                    $userOauth = $this->oauth->get_one(array('bind_uid' => $user_id));

                    if (md5(md5($oldpwd)) == $tmpPwd || empty($user['paypassword']) && $userOauth['id']) {
                        $res = $this->user->update(array('paypassword' => md5(md5($newpwd))), array('user_id' => $user_id));
                        if ($res) {
                            echo json_encode($ret);
                            exit;
                        } else {
                            $ret['msg'] = '修改失败';
                            echo json_encode($ret);
                            exit;
                        }
                    } else {
                        $ret['msg'] = '原始密码错误';
                        echo json_encode($ret);
                        exit;
                    }
                } else {
                    $ret['msg'] = '两次密码输入不一致';
                    echo json_encode($ret);
                    exit;
                }
            }
        } elseif ($act == 'mail') {

        }
        $data = $this->get_user_info('user_paypwd', 'pwd');

        $data['title'] = "交易密码" . ' - ' . CON_KEYWORDS;
        $this->load->view('user/user_paypwd', $data);
    }

    public function find() {
        if (!isset($_SESSION)) {
            session_start();
        }
        $this->load->helper('captcha');
        $act = $this->input->post('act');

        $user = $this->session->userdata('zjy_user');
        $data['user'] = $user;

        if ($act == 'mail') {
            $ret = array('msg' => '', 'url' => '');
            $captcha = $this->input->post('captcha');

            if ($captcha == "") {
                $ret['msg'] = '验证码不能为空';
                echo json_encode($ret);
                exit;
            } elseif ($captcha != $_SESSION['captcha']) {
                $ret['msg'] = '验证码不正确';
                echo json_encode($ret);
                exit;
            }
            $res = $this->Notice(3, $user['user_id']);
            if ($res) {
                //$this->js->alert('邮件发送成功，请从邮件内容里的链接地址重新设置交易密码，该链接有效时间1小时，请及时操作！');
                $ret['msg'] = '邮件发送成功，请从邮件内容里的链接地址重新设置交易密码，该链接有效时间1小时，请及时操作！';
            } else {
                //$this->js->alert('邮箱发送失败，请联系客服！');
                $ret['msg'] = '邮箱发送失败，请稍后再试或者联系客服！';
            }
            echo json_encode($ret);
            exit;
        }else if($act == 'phone'){
            $captcha1 = $this->input->post('captcha1');
            $ret = array('msg' => '', 'url' => '');
            if($captcha1 != $this->session->userdata('zjy_temp_captcha')){
                $ret['msg'] = '验证码不正确';
                echo json_encode($ret);
                exit;
            }
            if($captcha1 == ''){
                $ret['msg'] = '验证码不为空';
                echo json_encode($ret);
                exit;
            }
            $this->session->set_userdata('phone_set_pwd','set');
            $ret['msg'] = '';
            $ret['url'] = '/index.php/password/phone_paypwd';
            echo json_encode($ret);
            exit;
        }
        $data = $this->get_user_info('user_paypwd', 'pwd');
        $data['title'] = "密码找回" . ' - ' . CON_KEYWORDS;
        $this->load->view('user/user_find', $data);
    }

    function other($user_id = '') {
        //$this->js->go('/');
        $user_id = intval($user_id);
        $data = array();
        if ($user_id > 0) {
            $this->load->model('User_model');
            $this->load->model('Borrow_model');
            $this->load->model('User_vip_model');
            $this->load->model('Credit_model');

            $data['u'] = $this->User_model->get_by_uid($user_id);
            if (isset($data['u']['user_id']) && $data['u']['user_id'] > 0 && $data['u']['fstatus'] != 3) {
                if ($data['u']['province'] > 0) {
                    $this->load->model('Area_model');
                    $data['area_list'] = $this->Area_model->get_list();
                }
                $data['u_vip'] = $this->User_vip_model->get_by_uid($user_id);
                if (isset($data['u_vip']['vip_status']) && $data['u_vip']['vip_status'] == 1) {
                    $kefu_list = $this->User_model->get_kefu_list();
                    $data['u_vip']['username'] = isset($kefu_list[$data['u_vip']['kefu_userid']]) ? $kefu_list[$data['u_vip']['kefu_userid']]['username'] : '-';
                }
                $data['u_credit'] = $this->Credit_model->get_row($user_id);
                $data['u_credit']['credit_value'] = isset($data['u_credit']['credit_value']) ? $data['u_credit']['credit_value'] : 0;
                $data['u_credit']['pic'] = $this->Credit_model->get_pic($data['u_credit']['credit_value']);

                $privacy = $data['u']['privacy'] ? json_decode($data['u']['privacy'], true) : '';
                if (isset($privacy['tender']) && $privacy['tender'] == 0) {
                    $data['tender_list'] = array();
                } else {
                    $data['tender_list'] = $this->Borrow_model->get_borrow_tender_list(array('select' => 1, 'page_size' => 6, 'page_start' => 0, 'where' => array('user_id' => $user_id, 'fstatus' => 1)));
                }
                if (isset($privacy['borrow']) && $privacy['borrow'] == 0) {
                    $data['borrow_list'] = array();
                } else {
                    $data['borrow_list'] = $this->Borrow_model->get_borrow_list(array('select' => 2, 'list' => 1, 'page_size' => 6, 'page_start' => 0, 'where' => array('user_id' => $user_id))); //获取借款列表
                }
                $data['title'] = $data['u']['username'] . "的个人空间" . ' - ' . CON_KEYWORDS;
            } else {
                $data['msg'] = '页面不存在';
            }
        } else {
            $data['msg'] = '页面不存在';
        }
        $data['tab'] = $this->tab;
        $data['user_tab'] = 'other';

        $this->load->view('user/user_other', $data);
    }

    /**
     * 客服客户列表
     * @param $page
     */
    function customers() {
        $user = $this->session->userdata('zjy_user');
        $user_id = $user['user_id'];
        $this->load->model('User_model');
        $user = $this->User_model->get_by_uid($user_id);
        if ($user['type_id'] == 3) {
            $where['kefu_userid'] = $user_id;
            $page = $this->input->get('per_page') ? intval($this->input->get('per_page')) : 0;
            $where['page'] = $page;
            $where['epage'] = 20;
            $where['username'] = $this->input->get('username');
            $where['realname'] = $this->input->get('realname');
            $where['email'] = $this->input->get('email');
            $where['m_order'] = $this->input->get('m_order');
            $where['m_order'] = $where['m_order'] == 'collection' ? $where['m_order'] : 'reg_time';

            $this->load->model('User_vip_model');
            $result = $this->User_vip_model->get_customers($where);

            $this->load->library('pagination');
            $config['base_url'] = '/user/customers.html?username=' . $where['username'] . '&realname=' . $where['realname'] . '&email=' . $where['email'] . '&m_order=' . $where['m_order'];
            $config['total_rows'] = $result['count'];
            $config['per_page'] = 20;
            //$config['page_query_string'] = false;
            $config['first_link'] = '首页';
            $config['last_link'] = '尾页';
            $config['next_link'] = '下一页';
            $config['prev_link'] = '上一页';
            $this->pagination->initialize($config);

            $data = $this->get_user_info('', 'user_base');
            $data['list'] = $result['list'];
            $data['count'] = $result['count'];
            $this->load->model('Area_model', 'area');
            $data['area'] = $this->area->get_list();
            $data['where'] = $where;
            $data['title'] = "客户列表" . ' - ' . CON_KEYWORDS;
            $data['tab'] = $this->tab;
            $data['user_tab'] = 'customers';
            $data['user_sub'] = 'customers';
            $data['sub'] = 'invite';


            $this->load->view('user/user_customers', $data);
        } else {
            echo "You do not have permission to perform this operation"; //您没有权限执行此操作
            exit;
        }
    }

    /**
     * 客户当日投资明细
     */
    function ctender() {
        $user = $this->session->userdata('zjy_user');
        $user_id = $user['user_id'];
        $this->load->model('User_model');
        $user = $this->User_model->get_by_uid($user_id);
        if ($user['type_id'] == 3) {
            $page = $this->input->get('per_page') ? intval($this->input->get('per_page')) : 0;

            $this->load->model('User_vip_model');
            $result = $this->User_vip_model->get_customers_tender($user_id, $page);

            $this->load->library('pagination');
            $config['base_url'] = '/user/ctender.html?epage=20';
            $config['total_rows'] = $result['count'];
            $config['per_page'] = 20;
            //$config['page_query_string'] = false;
            $config['first_link'] = '首页';
            $config['last_link'] = '尾页';
            $config['next_link'] = '下一页';
            $config['prev_link'] = '上一页';
            $this->pagination->initialize($config);
            $data['list'] = $result['list'];
            $data['count'] = $result['count'];
            $data['tab'] = $this->tab;
            $data['user_tab'] = 'ctender';
            $data['sub'] = 'invite';
            $data['title'] = "客户投资明细" . ' - ' . CON_KEYWORDS;
            $this->load->view('user/user_customers_tender', $data);
        } else {
            echo "You do not have permission to perform this operation"; //您没有权限执行此操作
            exit;
        }
    }

    /**
     * 客服客户投资统计
     */
    function customers_invest() {
        $user = $this->session->userdata('zjy_user');
        $user_id = $user['user_id'];
        $data = array();
        $date = date("Y-m-01");
        $month1 = date("Y-m-d", strtotime("$date -1 month"));
        $month2 = date("Y-m-d", strtotime($date));
        $month3 = date("Y-m-d", strtotime("$month2 +1 month"));
        $this->load->model('User_vip_model');
        $where['kefu_userid'] = $user_id;

        $start = $this->input->post('start');
        $end = $this->input->post('end');
        $data['parms']['start'] = $start;
        $data['parms']['end'] = $end;
        if ($start && $end) {
            $where['start'] = strtotime($start);
            $where['end'] = strtotime($end);
            $total = $this->User_vip_model->get_invest_sum($where);
            $data['list'][] = array(
                'start' => $start,
                'end' => $end,
                'total' => $total
            );
        } else {
            $where['start'] = strtotime($month1);
            $where['end'] = strtotime($month2);
            $total = $this->User_vip_model->get_invest_sum($where);
            $data['list'][] = array(
                'start' => $month1,
                'end' => $month2,
                'total' => $total
            );
            $where['start'] = strtotime($month2);
            $where['end'] = strtotime($month3);
            $total = $this->User_vip_model->get_invest_sum($where);
            $data['list'][] = array(
                'start' => $month2,
                'end' => $month3,
                'total' => $total
            );
        }
        $data['tab'] = $this->tab;
        $data['user_tab'] = 'customers_invest';
        $data['sub'] = 'invite';
        $data['title'] = "客户投资" . ' - ' . CON_KEYWORDS;
        $this->load->view('user/user_customers_invest', $data);
    }

    /**
     * 客服客户投资统计
     */
    function customers_stat() {
        $user = $this->session->userdata('zjy_user');
        $user_id = $user['user_id'];
        $this->load->model('Account_model');
        $customers_stat_list = $this->Account_model->kefu_customers_stat($user_id);

        $vdata['customers_stat_list'] = $customers_stat_list;
        $vdata['tab'] = $this->tab;
        $vdata['user_tab'] = 'customers_stat';
        $vdata['sub'] = 'invite';
        $vdata['title'] = "客户投资" . ' - ' . CON_KEYWORDS;
        $this->load->view('user/user_customers_stat', $vdata);
    }

    function invite() {
        $data = array();
        $user = $this->session->userdata('zjy_user');
        $user_id = $user['user_id'];
        $this->load->model('User_model');
        $data = $this->get_user_info('tuijian_haoyou', 'tuijian', 'invite');
        $data['invite_user_list'] = $this->User_model->get_invite_list($user_id);
        $data['invite_user_list'] = $this->get_reward_money($user_id,$data['invite_user_list'],1);

        $elist = array();
        foreach ($data['invite_user_list'] as $ui) {
            $telist = $this->User_model->get_invite_list($ui['user_id']);
            foreach ($telist as $tl) {
                $elist[] = $tl;
            }
        }
        $data['elist'] = $elist;
        $data['elist'] = $this->get_reward_money($user_id,$data['elist'],2);

        $data['title'] = "邀请好友" . ' - ' . CON_KEYWORDS;
        $data['tab_first'] = 'recommend';
        $data['tab_second'] = 'recommend_friend';
        $this->load->view('user/user_invite', $data);
    }

    //批量获得奖励金额
    function get_reward_money($user_id,$user_list,$type){
        $this->load->model('Borrow_model');
        //作为一级推荐人的奖励
        if($type == 1){
            $yi_user_list = array();
            foreach($user_list as $list){
                $list['yiji_reward'] = $this->Borrow_model->yiji_reward($list['user_id'],$user_id);
                $yi_user_list[] = $list;
            }
            return $yi_user_list;
        }

        //作为顶级推荐人的奖励
        if ($type == 2){
            $ding_user_list = array();
            foreach($user_list as $list){
                $list['dingji_reward'] = $this->Borrow_model->dingji_reward($list['user_id'],$user_id);
                $ding_user_list[] = $list;
            }
            return $ding_user_list;
        }
    }

    function creditlog() {
        $user = $this->session->userdata('zjy_user');
        if (isset($user['user_id']) && intval($user['user_id']) > 0) {
            $page = $this->input->get('per_page');
            $page = intval($page);
            $user_id = $user['user_id'];
            $data = array();
            $this->load->model('Credit_model');
            $where = array('user_id' => $user_id);
            $result = $this->Credit_model->get_credit_list($where, $page);

            $data = $this->get_user_info('', '', '');

            $data['creditTypeName'] = $this->Credit_model->get_credit_rule_list_name();

            $this->load->library('pagination');
            $config['base_url'] = '/user/creditlog.html?page_size=20';
            $config['total_rows'] = $result['count'];
            $config['per_page'] = 20;
            $config['first_link'] = '首页';
            $config['last_link'] = '尾页';
            $config['next_link'] = '下一页';
            $config['prev_link'] = '上一页';
            //$config['display_pages'] = FALSE;
            //$config['num_links'] = 0;
            $this->pagination->initialize($config);
            //echo count($msg['list']);exit;

            $data['list'] = $result['list'];
            //$data['no_read'] = $no_read_count;
            $data['title'] = "积分日志" . ' - ' . CON_KEYWORDS;
            $data['credit_menu'] = 1;
            $this->load->view('user/user_credit_list', $data);
        } else {
            header('Location: /login.html');
        }
    }

    function orderlist() {
        $user = $this->session->userdata('zjy_user');
        if (isset($user['user_id']) && intval($user['user_id']) > 0) {
            $this->load->model('Shop_model');
            $page = $this->input->get('per_page');
            $page = intval($page);
            $page_size = 10;
            $data = array(
                'page' => $page,
                'page_size' => $page_size,
                'order' => 'add_time',
                'where' => array(
                    'user_id' => $user['user_id']
                )
            );
            $ret = $this->Shop_model->orderPageList($data);
            //var_dump($ret);
            $this->load->library('pagination');
            $config['base_url'] = '/user/orderlist.html?page_size=' . $page_size;
            $config['total_rows'] = $ret['count'];
            $config['per_page'] = $page_size;
            $config['first_link'] = '首页';
            $config['last_link'] = '尾页';
            $config['next_link'] = '下一页';
            $config['prev_link'] = '上一页';
            //$config['display_pages'] = FALSE;
            //$config['num_links'] = 0;
            $this->pagination->initialize($config);

            $vdata = $this->get_user_info('', '', '');

            $vdata['olist'] = $ret['list'];
            $vdata['title'] = "商城兑换记录" . ' - ' . CON_KEYWORDS;
            $vdata['credit_menu'] = 2;
            $this->load->view('user/user_order_list', $vdata);
        } else {
            header('Location: /login.html');
        }
    }

    private function safe_leve($user) {
        $this->load->model('User_model', 'user');
        $this->load->model('User_vip_model', 'vip');
        $this->load->model('Account_bank_model', 'bank');
        $this->load->model('Account_model', 'account');


        if ($user == false) {
            $user['paypassword'] = "";
            $user['real_status'] = 0;
            $user['phone_status'] = 0;
            $user['email_status'] = 0;
        }

        $user['bank_status'] = 0;
        $user['vip_status'] = 0;
        $addup = 1;

        if ($user['real_status'] == 1) {
            $addup = $addup + 1;
        }

        if ($user['phone_status'] == 1) {
            $addup = $addup + 1;
        }

        if ($user['email_status'] == 1) {
            $addup = $addup + 1;
        }

        $bankinfo = $this->bank->get_by_uid($user['user_id']);
        if ($bankinfo) {
            $user['bank_status'] = 1;
            $addup = $addup + 1;
        }

        $vipinfo = $this->vip->get_by_uid($user['user_id']);
        if ($vipinfo && $vipinfo['vip_status'] == 1) {
            $user['vip_status'] = 1;
            $addup = $addup + 1;
        }

        if ($user['paypassword'] != "") {
            $addup = $addup + 1;
        }

        return array('user' => $user, 'user_vip' => $vipinfo, 'addup' => intval($addup));
        //return intval($addup);
    }

    public function user_safe() {

        $this->load->model('User_model', 'user');
        $this->load->model('User_vip_model', 'vip');
        $this->load->model('Account_bank_model', 'bank');
        $this->load->model('Account_model', 'account');


        $user = $this->session->userdata('zjy_user');

        if (isset($user['user_id']) && intval($user['user_id']) > 0) {

            $user = $this->user->get_by_uid($user['user_id']);

            $safeinfo = $this->safe_leve($user);

            $data['addup'] = $safeinfo['addup'];
            $data['user'] = $safeinfo['user'];
            $data['user_vip'] = $safeinfo['user_vip'];
            $data['account'] = $this->account->get_account($user['user_id']);

            if (!$data['account']) {
                $initAccount = array(
                    'user_id' => $user['user_id'],
                    'total' => '0',
                    'use_money' => '0',
                    'no_use_money' => '0',
                    'collection' => '0',
                );

                //$initAccount['total'] = 100;
                //$initAccount['reg_money'] = 100;

                $account_id = $this->account->init_account($initAccount);
                if ($account_id)
                    $initAccount['id'] = $account_id;
                $data['account'] = $initAccount;
            }

            $data['title'] = "安全中心" . ' - ' . CON_KEYWORDS;
            $data['tab'] = $this->tab;
            $data['sub'] = $this->sub;
            $data['user_tab'] = 'user';
            $data['user_sub'] = 'safe';
            $data['tab_first'] = 'account';
            $data['tab_second'] = 'account_safe';

            $this->load->view('user/user_safe', $data);
        }else {
            header('Location: /login.html');
        }
    }

    public function searchMmm($mmm_id) {
        $this->load->library('/mmm/Rsa', 'rsa');
        $this->load->library('/mmm/Common', 'common');

        $post_data = array();
        $post_data['PlatformId'] = $mmm_id;
        $post_data['PlatformType'] = "1";
        $post_data['PlatformMoneymoremore'] = MONEYMOREMOREPID;
        $dataStr = $post_data['PlatformId'] . $post_data['PlatformType'] . $post_data['PlatformMoneymoremore'];
        if ($this->common->getAntiState() == 1) {
            $dataStr = strtoupper(md5($dataStr));
        }
        $post_data['SignInfo'] = $this->rsa->sign($dataStr);

        $url = MONEYMOREMOREURL . '/loan/balancequery.action';
        $o = "";
        foreach ($post_data as $k => $v) {
            $o.= "$k=" . urlencode($v) . "&";
        }

        $post_data = substr($o, 0, -1);

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);


        //为了支持cookie
        curl_setopt($ch, CURLOPT_COOKIEJAR, 'cookie.txt');
        curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
        $result = curl_exec($ch);
        return $result;
    }

    function setlogincredict(){
        $user_id = intval($this->input->post('user_id'));
        $this->load->model('Credit_model','credit');
        echo $this->credit->give_login_credit($user_id);
    }

}
